package com.numberone.master.modules.news.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.numberone.master.common.config.Global;
import com.numberone.master.common.persistence.Page;
import com.numberone.master.common.web.BaseController;
import com.numberone.master.common.utils.StringUtils;
import com.numberone.master.modules.news.entity.News;
import com.numberone.master.modules.news.service.NewsService;
import com.numberone.master.modules.recharge.entity.RechargeLs;
import com.numberone.master.modules.sys.entity.Office;
import com.numberone.master.modules.sys.service.OfficeService;
import com.numberone.master.modules.sys.utils.UserUtils;

/**
 * 消息推送Controller
 * @author chengkang
 * @version 2018-02-23
 */
@Controller
@RequestMapping(value = "${adminPath}/news/news")
public class NewsController extends BaseController {

	@Autowired
	private NewsService newsService;
	
	@Autowired
	private OfficeService officeService;

	 /**
     * 根据id查询一条数据
     * @param id
     * @return
     */
	@ModelAttribute
	public News get(@RequestParam(required=false) String id) {
		News entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = newsService.get(id);
		}
		if (entity == null){
			entity = new News();
		}
		return entity;
	}

	 /**
     * list列表
     * @param news
     * @param request
     * @param response
     * @param model
     * @return
     */
	@RequiresPermissions("news:news:list")
	@RequestMapping(value = {"list", ""})
	public String list(News news, HttpServletRequest request, HttpServletResponse response, Model model) {
		//获取当前登录人的id
		String id = UserUtils.getUser().getOffice().getId();
		news.setPushNewsId(id);
		Page<News> page = newsService.findPage(new Page<News>(request, response), news); 
		model.addAttribute("page", page);
		return "modules/news/newsList";
	}
	
	/**
	 * 已度消息
	 * @param news
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("news:read:list")
	@RequestMapping(value = {"read_news_list", ""})
	public String readNewsList(News news, HttpServletRequest request, HttpServletResponse response, Model model) {
		//获取当前登录人的id
		String id = UserUtils.getUser().getOffice().getId();
		news.setAcceptUserId(id);
		news.setSeeState("1");
		Page<News> page = newsService.findPage(new Page<News>(request, response), news); 
		model.addAttribute("page", page);
		return "modules/news/newsReadList";
	}
	
	/**
	 * 未读消息
	 * @param news
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("news:unread:list")
	@RequestMapping(value = {"unread_news_list", ""})
	public String unreadNewsList(News news, HttpServletRequest request, HttpServletResponse response, Model model) {
		//获取当前登录人的id
		String id = UserUtils.getUser().getOffice().getId();
		news.setAcceptUserId(id);
		news.setSeeState("0");
		Page<News> page = newsService.findPage(new Page<News>(request, response), news); 
		model.addAttribute("page", page);
		return "modules/news/newsUnreadList";
	}
	
	/**
	 * 批量阅读
	 * @param ids 阅读id
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("news:news:read")
	@RequestMapping(value = "read")
	public String read(String[] ids,RedirectAttributes redirectAttributes) {
			int num = 0;
			for (int i = 0; i < ids.length; i++) {
				News news = new News();
				news.setId(ids[i]);
				news.setSeeDateTime(new Date());
				news.setSeeState("1");//已读
				newsService.update(news);
				num++;
			}
			addMessage(redirectAttributes, "成功阅读了"+num+"条信息");
			return "redirect:"+adminPath+"/news/news/unread_news_list?repage";
	}

	/**
     * 打开增加页面
     * @return
     */
	@RequiresPermissions("news:news:save")
	@RequestMapping(value = "openSave")
	public String openSave() {
		return "modules/news/newsSave";
	}


	/**
	 * 打开修改页面
	 * @param news
	 * @param model
	 * @return
	 */
	@RequiresPermissions("news:news:update")
	@RequestMapping(value = "openUpdate")
	public String openUpdate(News news, Model model) {
		model.addAttribute("news", news);
		return "modules/news/newsUpdate";
	}

	/**
	 * 添加
	 * @param news
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("news:news:save")
	@RequestMapping(value = "save")
	public String save(News news, Model model, RedirectAttributes redirectAttributes) {
		//获取当前登录人的id
		int num = 0;
		String id = UserUtils.getUser().getOffice().getId();
		List<Office> list = new ArrayList<>();
		Office office = new Office();
		office.setDelFlag("0");
		if (!beanValidator(model, news)){
			return openSave();
		}
		if("0".equals(news.getAcceptUserType())){//全部
			list = officeService.findList(office);
		}else if("1".equals(news.getAcceptUserType())){//所有代理商用户
			office.setType("2");
			list = officeService.findList(office);
		}else if("2".equals(news.getAcceptUserType())){//所有个人用户
			office.setType("1");
			list = officeService.findList(office);
		}
		for (int i = 0; i < list.size(); i++) {
			news.setPushNewsId(id);
			news.setAcceptUserId(list.get(i).getId());
			news.setCreateDate(new Date());
			news.setSeeState("0");
			newsService.insert(news);
			num++;
		}
		addMessage(redirectAttributes, "成功推送'"+num+"'条消息");
		return "redirect:"+Global.getAdminPath()+"/news/news/list?repage";
	}


	 /**
     * 删除
     * @param news
     * @param redirectAttributes
     * @return
     */
	@RequiresPermissions("news:news:delete")
	@RequestMapping(value = "delete")
	public String delete(News news, RedirectAttributes redirectAttributes) {
		newsService.delete(news);
		addMessage(redirectAttributes, "删除消息成功");
		return "redirect:"+Global.getAdminPath()+"/news/news/?repage";
	}

}